package biblioGraphiqueV2;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Observable;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

public class VueParametrerQuotas extends Vue {
private static final long serialVersionUID = 1L;

	private JPanel contentPane;
	// TextField : ////////////////////////
	private JTextField textFieldMaxEmprunt;
	private JTextField textFieldMaxRes;
	private JTextField textFieldDureEmprunt;
	private JTextField textFieldValRes;
	private JTextField textFieldPenalite;
	// TextButton : ///////////////////////
	private JButton buttonValider;
	private JButton buttonAnnuler;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	
	
	/**
	 * Constructeur de la fenêtre
	 */
	public VueParametrerQuotas(Controleur controleur) {
		super(controleur);
		setTitle("Paramétrage des quotas");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
		setBounds(100, 100, 539, 287);
		contentPane = new JPanel();
		this.addWindowListener (new WindowAdapter() {
			public void windowClosing (WindowEvent e) {
			getControleur().fermerVue(VueParametrerQuotas.this);
			}
		});
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		/////////Emprunt Max :///////////////////////////////////////////////////////////////////
		JLabel lblNewLabel = new JLabel("Emprunt Maximum (en jours) ");
		lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNewLabel.setBounds(0, 56, 307, 15);
		contentPane.add(lblNewLabel);
		
		textFieldMaxEmprunt = new JTextField();
		textFieldMaxEmprunt.setBounds(424, 54, 95, 19);
		contentPane.add(textFieldMaxEmprunt);
		textFieldMaxEmprunt.setColumns(10);
		/////////Reservation Max :///////////////////////////////////////////////////////////////////
		JLabel lblNewLabel_1 = new JLabel("Réservation Maximum (en jours) ");
		lblNewLabel_1.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNewLabel_1.setBounds(-5, 87, 312, 15);
		contentPane.add(lblNewLabel_1);
		
		textFieldMaxRes = new JTextField();
		//textFieldMaxRes.setEditable(false);
		textFieldMaxRes.setBounds(424, 85, 95, 19);
		contentPane.add(textFieldMaxRes);
		textFieldMaxRes.setColumns(10);
		///////////DureEmprunt ://///////////////////////////////////////////////////////////////
		textFieldDureEmprunt = new JTextField();
		textFieldDureEmprunt.setColumns(10);
		textFieldDureEmprunt.setBounds(424, 116, 95, 19);
		contentPane.add(textFieldDureEmprunt);
		
		JLabel lblDureEmprunt = new JLabel("Durée Emprunt (en jours) ");
		lblDureEmprunt.setHorizontalAlignment(SwingConstants.RIGHT);
		lblDureEmprunt.setBounds(0, 118, 307, 15);
		contentPane.add(lblDureEmprunt);
		//////////Validite reservation ://////////////////////////////////////////////////////////////////
		textFieldValRes = new JTextField();

		textFieldValRes.setColumns(10);
		textFieldValRes.setBounds(424, 147, 95, 19);
		contentPane.add(textFieldValRes);
		
		JLabel lblValRes = new JLabel("Validité Réservation (en jours) ");
		lblValRes.setHorizontalAlignment(SwingConstants.RIGHT);
		lblValRes.setBounds(-5, 149, 312, 15);
		contentPane.add(lblValRes);
		//////////penalite journaliere ://////////////////////////////////////////////////////////////////
		textFieldPenalite = new JTextField();
		textFieldPenalite.setColumns(10);
		textFieldPenalite.setBounds(424, 178, 95, 19);
		contentPane.add(textFieldPenalite);
		
		JLabel lblPenalite = new JLabel("Montant Pénalite Journalière (en euros) ");
		lblPenalite.setHorizontalAlignment(SwingConstants.RIGHT);
		lblPenalite.setBounds(-6, 180, 313, 15);
		contentPane.add(lblPenalite);
		/////////Button :///////////////////////////////////////////////////////////////////
		buttonAnnuler = new JButton("Annuler");
		buttonAnnuler.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			getControleur().fermerVue(VueParametrerQuotas.this);}
		});
		buttonAnnuler.setBounds(325, 224, 107, 25);
		contentPane.add(buttonAnnuler);
		
		buttonValider = new JButton("Valider");
		buttonValider.setSize(107, 25);
		buttonValider.setLocation(206, 224);
		buttonValider.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getControleur().nouvQuota(textFieldMaxEmprunt.getText(),textFieldPenalite.getText() ,
						                  textFieldValRes.getText() ,textFieldMaxRes.getText(), textFieldDureEmprunt.getText());
				}
		});
		contentPane.add(buttonValider);
		
		JLabel lblActuel = new JLabel("Actuel");
		lblActuel.setBounds(340, 27, 61, 15);
		contentPane.add(lblActuel);
		
		JLabel lblNouvelle = new JLabel("Nouveaux");
		lblNouvelle.setBounds(441, 27, 61, 15);
		contentPane.add(lblNouvelle);
		
		textField = new JTextField();
		textField.setEditable(false);
		textField.setBounds(325, 54, 87, 19);
		contentPane.add(textField);
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setEditable(false);
		textField_1.setColumns(10);
		textField_1.setBounds(325, 85, 87, 19);
		contentPane.add(textField_1);
		
		textField_2 = new JTextField();
		textField_2.setEditable(false);
		textField_2.setColumns(10);
		textField_2.setBounds(325, 116, 87, 19);
		contentPane.add(textField_2);
		
		textField_3 = new JTextField();
		textField_3.setEditable(false);
		textField_3.setColumns(10);
		textField_3.setBounds(325, 147, 87, 19);
		contentPane.add(textField_3);
		
		textField_4 = new JTextField();
		textField_4.setEditable(false);
		textField_4.setColumns(10);
		textField_4.setBounds(325, 178, 87, 19);
		contentPane.add(textField_4);		
		alimOldQuota(controleur);
		/////////////////////////////////////////////////////////////////////////////////
		}

	/**
	 * définition des boutons.
	 */
	private void alimOldQuota(Controleur cont)
	{	
		Quota q = cont.getQuota();
		if(q != null)
		{
	
			textField.setText(String.valueOf(q.getMaxEmprunt()));
			textField_1.setText(String.valueOf(q.getMaxReservation()));
			textField_2.setText(String.valueOf(q.getDuree()));
			textField_3.setText(String.valueOf(q.getValiditeRes()));
			textField_4.setText(String.valueOf(q.getPenalite()));
		}
		else{
			textField.setText("0");
			textField_1.setText("0");
			textField_2.setText("0");
			textField_3.setText("0");
			textField_4.setText("0");
		}
		
	}
	public void setEtat (int etat){
			buttonValider.setEnabled(true);
			buttonAnnuler.setEnabled(true);		
	}
	
}